#
# @lc app=leetcode.cn id=202 lang=python3
#
# [202] 快乐数
#

# @lc code=start
class Solution:
    def isHappy(self, n: int) -> bool:
        s = set()
        while n != 1:
            if n in s:
                return False
            s.add(n)
            n = self.genNext(n)
        return True
    
    def genNext(self, n: int) -> int:
        ans = 0
        while n > 0:
            ans += (n % 10) ** 2
            n = n // 10
        return ans
# @lc code=end

